/*
 * $Id: RExecCommand.java,v 1.9 2010/03/17 23:37:19 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.commands;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

import org.apache.commons.net.bsd.RExecClient;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.exceptions.RExecCommandException;

/**
 * @author 212042764
 * 
 */
public class RExecCommand extends RExecClient {
	private String username = null;
	private String passcode = null;
	private String hostname = null;

	/**
	 * AutoSC always connect through clientdrv so that rexec protocol can be
	 * kept active for the duration of activity
	 * 
	 * @param hostname
	 * @param username
	 * @param password
	 * @param timeoutMillis
	 * @throws RExecCommandException
	 * @throws IOException
	 * @throws SocketException
	 * @throws ClientDrvCommandException
	 */
	public void connect(String hostname, String username, String password,
			int timeoutMillis) throws RExecCommandException {
		this.username = username;
		this.passcode = password;
		this.hostname = hostname;
		try {
			this.setDefaultTimeout(timeoutMillis);
			this.connect(hostname);
			this.setSoTimeout(timeoutMillis);
		} catch (SocketException socketException) {
			throw new RExecCommandException("Failed to connect to hostname:"
					+ hostname, socketException);
		} catch (IOException ioException) {
			throw new RExecCommandException("Failed to connect to hostname:"
					+ hostname, ioException);
		}
	}

	/**
	 * Connect to the client using a existing socket
	 * 
	 * @param socket
	 * @param timeout
	 * @throws IOException
	 * @throws ClientDrvCommandException
	 */
	public void connect(Socket socket, int timeout) throws IOException {
		this._timeout_ = timeout;
		this._socket_ = socket;
		this._connectAction_();
	}

	/**
	 * Executes the command
	 * 
	 * @param command
	 * @throws RExecCommandException
	 * @throws IOException
	 */
	public void executeCommand(String command) throws RExecCommandException {
		try {
			this.rexec(username, passcode, command);
		} catch (IOException ioException) {
			throw new RExecCommandException("Failed to execute command:"
					+ command, ioException);
		}
	}

	/**
	 * Resets socket timeout to passed value
	 * 
	 * @param timeoutMillis
	 * @throws RExecCommandException
	 */
	public void setTimeout(int timeoutMillis) throws RExecCommandException {
		try {
			this.setSoTimeout(timeoutMillis);
		} catch (SocketException socketException) {
			throw new RExecCommandException("Failed to set timeout to:"
					+ timeoutMillis, socketException);
		}
	}

	/**
	 * Disconnects from clientdrv otherwise the session will remain open and
	 * timeout after some time
	 */
	public void disconnect() {
		if (this.isConnected()) {
			try {
				super.disconnect();
			} catch (IOException e) {
				ASCLogger.info("Failed to disconnect from host:" + hostname);
			}
		}
	}

	/**
	 * @return the outputStream
	 */
	public OutputStream getOutputStream() {
		return super.getOutputStream();
	}

	/**
	 * @return the inputStream
	 */
	public InputStream getInputStream() {
		return super.getInputStream();
	}
}
